Skip to content

Update MSC4311 tests to conform to the spec and better coverage#796

Open
turt2live wants to merge 21 commits into
mainfrom
travis/msc4311-v2
Open

Update MSC4311 tests to conform to the spec and better coverage#796
turt2live wants to merge 21 commits into
mainfrom
travis/msc4311-v2

Conversation

@turt2live
Copy link
Copy Markdown
Member

@turt2live turt2live commented Aug 15, 2025

Update MSC4311 tests to conform to the spec. MSC4311 states that the client API should still use the stripped state event format. For the federation API, it should use full PDU's. MSC4311 also requires the m.room.create event be included in the list of stripped state.

"Stripped state" is a generic term that refers to simplified slice of state shared in invite_room_state/knock_room_state (invite_state/knock_state with /sync) before someone is joined to a room and can see the full room state.

This PR was originally authored by @turt2live taking a stab at fixing single the flawed test (self-reported: "I have no idea what I'm doing" disclaimer goes here) but has since been taken over by @MadLittleMods. And has naturally evolved some new better test coverage as I've worked on a full solution.

Synapse PR: element-hq/synapse#18822 element-hq/synapse#19723

The previous tests passed because of a flawed implementation in Synapse which is being removed in element-hq/synapse#19723 as well.

Dev notes

Running the tests with Synapse:

COMPLEMENT_DIR=../complement ./scripts-dev/complement.sh -run TestMSC4311StrippedStateClientAPI

COMPLEMENT_DIR=../complement ./scripts-dev/complement.sh -run TestMSC4311FullEventsOnStrippedStateFederation

COMPLEMENT_DIR=../complement ./scripts-dev/complement.sh -run TestMSC4311RejectInvalidStrippedStateFederation

Comment thread tests/v12_test.go Outdated
must.MatchGJSON(t, ev,
match.JSONKeyPresent("origin_server_ts"),
)
srv.Mux().HandleFunc("/_matrix/federation/v2/invite/{roomID}/{eventID}", srv.ValidFederationRequest(t, func(fr *fclient.FederationRequest, pathParams map[string]string) util.JSONResponse {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should be able to alternatively use:

	srv := federation.NewServer(t, deployment,
		federation.HandleInviteRequests(func(p gomatrixserverlib.PDU) {
			// checks here
		}),
		federation.HandleKeyRequests(),
		federation.HandleMakeSendJoinRequests(),
		federation.HandleTransactionRequests(nil, nil),
	)

Copy link
Copy Markdown
Collaborator

@MadLittleMods MadLittleMods May 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went down this route but the problem is that federation.HandleInviteRequests(...) doesn't allow you to inspect the invite_room_state yet. It would have to be updated to pass in the full invite request to the callback.

And gomatrixserverlib needs to be updated to handle the new world where invite_room_state can be stripped state or full PDUs.

We could have an infallible InviteV2Request.StrippedInviteRoomState() that would give a view of stripped state regardless of whether we were passed stripped or full PDU's (we can derive stripped state from the full PDU's). And then a fallible InviteV2Request.FullInviteRoomState() that would return an error if the homeserver didn't pass us full PDUs.

We would also need to update IRoomVersion to add new fields like CreateEventRequiredInInviteRoomState and FullPDUInviteRoomState (full PDU's can happen in any room version) so we can conditionally apply this behavior. Or maybe the flags could be combined as MSC4311InviteRoomState

If any of the events are not a PDU, not for the room ID specified, or fail signature checks, or the m.room.create event is missing, the receiving server MAY respond to invites with a 400 M_MISSING_PARAM standard Matrix error (new to the endpoint). For invites to room version 12+ rooms, servers SHOULD rather than MAY respond to such requests with 400 M_MISSING_PARAM.

-- matrix-org/matrix-spec-proposals#4311

Comment thread tests/v12_test.go Outdated
Comment thread tests/v12_test.go Outdated
Comment thread tests/v12_test.go Outdated
Comment thread tests/v12_test.go
Comment thread tests/v12_test.go
Comment on lines +1646 to +1650
// TODO: Test events not full PDU's

// TODO: Test events not from the same room

// TODO: Test invalid signatures/hashes
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something for future PRs

Comment thread tests/v12_test.go
// > error (new to the endpoint). For invites to room version 12+ rooms, servers
// > SHOULD rather than MAY respond to such requests with `400 M_MISSING_PARAM`.
func TestMSC4311RejectInvalidStrippedStateFederation(t *testing.T) {
runtime.SkipIf(t, runtime.Synapse) // FIXME: Run these tests after 2027-06-01
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also cross-linked this in the Synapse codebase (see element-hq/synapse#19723)

@MadLittleMods MadLittleMods changed the title Try to modify MSC4311 test to meet new proposal Update MSC4311 tests to conform to the spec May 27, 2026
@MadLittleMods MadLittleMods changed the title Update MSC4311 tests to conform to the spec Update MSC4311 tests to conform to the spec and better coverage May 27, 2026
@MadLittleMods MadLittleMods marked this pull request as ready for review May 27, 2026 21:04
@MadLittleMods MadLittleMods requested review from a team as code owners May 27, 2026 21:04
@anoadragon453 anoadragon453 requested a review from Copilot May 28, 2026 15:13
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates MSC4311 coverage for room version 12 by replacing an incorrect stripped-state test with client-server, federation, and rejection coverage, plus helper support for knock flows.

Changes:

  • Adds MSC4311 tests for client /sync stripped state and federation invite/knock stripped state.
  • Adds JSONArraySome matcher for asserting that at least one JSON array element matches.
  • Adds client and federation knock helpers used by the new tests.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
tests/v12_test.go Replaces the old MSC4311 test with expanded client, federation, and invalid-state rejection tests.
match/json.go Adds JSONArraySome matcher for “any element matches” assertions.
federation/server.go Adds federation knock helper and strict knock_room_state validation option.
client/client.go Adds client knock helper and fixes invite helper comments.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread match/json.go
var satisifed bool = false
body.ForEach(func(_, val gjson.Result) bool {
err := fn(val)
satisifed = err != nil
Comment thread match/json.go
}

// JSONArraySome returns a matcher which will check that `wantKey` is an array then
// loops over each item calling `fn`. If `fn` returns nil, the matcher is satisifed,
Comment thread client/client.go
serverNameStrings[i] = string(serverName)
}
query := url.Values{
"via": serverNameStrings,
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants